﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using CLoggerNamespace;

namespace HL7v_2x
{
    public class HL7SegmentMSH : IHL7Segment
    {
        #region DESARMAR SEGMENTO EN CAMPOS
        public List<HL7Fields> DesarmarSegmentoEnCampos(string strSegment)
          {
             List<HL7Fields> lstHL7Fields = new List<HL7Fields>();
              //EJEMPLO MENSAJE HL7
              /*
                   MSH|^~\&|HIG||CPS||20050426113700||ADT^A01^ADT_A01|HIG9744495|P|2.5||||AL<cr>
              */

              string[] strArrAux = null;

              try
              {
                  strArrAux = strSegment.Split('|');
                  int inCantBarras = strArrAux.Length - 1;
                  CLogger.Append("Cantidad de Barras: " + inCantBarras + " - MAXIMO DE 21 CAMPOS.", CLogger.INFO);
              }
              catch(Exception ex)
              {

                  throw new Exception("ERROR PARCEANDO LAS BARRAS | Ex: " + ex.Message);
              }
              try
              {

                  if ((strArrAux.Length > 0) && (strArrAux[0] != null) && (strArrAux[0] != ""))    // FieldSeparator_MSH = 00001,  
                  {
                      CLogger.Append("Field 0: " + strArrAux[0], CLogger.INFO);
                      System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                      lstHL7Fields.Add(new HL7Fields(HL7FieldName.FieldSeparator_MSH, enc.GetBytes(strArrAux[0])));


                  }
                  if ((strArrAux.Length > 1) && (strArrAux[1] != null) && (strArrAux[1] != null))  // EncodingCharacters_MSH = 00002,
                  {
                      CLogger.Append("Field 1: " + strArrAux[1], CLogger.INFO);
                      System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                      lstHL7Fields.Add(new HL7Fields(HL7FieldName.EncodingCharacters_MSH, enc.GetBytes(strArrAux[1])));
                  }
                  if ((strArrAux.Length > 2) && (strArrAux[2] != "") && (strArrAux[2] != null)) // SendingApplication_MSH = 00003,
                  {
                      CLogger.Append("Field 2: " + strArrAux[2], CLogger.INFO);
                      System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                      lstHL7Fields.Add(new HL7Fields(HL7FieldName.SendingApplication_MSH, enc.GetBytes(strArrAux[2])));
                  }
                  if ((strArrAux.Length > 3) && (strArrAux[3] != "") && (strArrAux[3] != null)) // SendingFacility_MSH = 00004,
                  {
                      CLogger.Append("Field 3: " + strArrAux[3], CLogger.INFO);
                      System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                      lstHL7Fields.Add(new HL7Fields(HL7FieldName.SendingFacility_MSH, enc.GetBytes(strArrAux[3])));
                  }
                  if ((strArrAux.Length > 4) && (strArrAux[4] != "") && (strArrAux[4] != null)) // ReceivingFacility_MSH = 00005,
                  {
                      CLogger.Append("Field 4: " + strArrAux[4], CLogger.INFO);
                      System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                      lstHL7Fields.Add(new HL7Fields(HL7FieldName.ReceivingFacility_MSH, enc.GetBytes(strArrAux[4])));
                  }
                  if ((strArrAux.Length > 5) && (strArrAux[5] != "") && (strArrAux[5] != null)) // ReceivingApplication_MSH = 00006,
                  {
                      CLogger.Append("Field 5: " + strArrAux[5], CLogger.INFO);
                      System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                      lstHL7Fields.Add(new HL7Fields(HL7FieldName.ReceivingApplication_MSH, enc.GetBytes(strArrAux[5])));
                  }
                  if ((strArrAux.Length > 6) && (strArrAux[6] != "") && (strArrAux[6] != null)) // DateTimeofMessage_MSH = 00007,
                  {
                      CLogger.Append("Field 6: " + strArrAux[6], CLogger.INFO);
                      System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                      lstHL7Fields.Add(new HL7Fields(HL7FieldName.DateTimeofMessage_MSH, enc.GetBytes(strArrAux[6])));
                  }
                  if ((strArrAux.Length > 7) && (strArrAux[7] != "") && (strArrAux[7] != null)) // Security_MSH = 00008,
                  {
                      CLogger.Append("Field 7: " + strArrAux[7], CLogger.INFO);
                      System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                      lstHL7Fields.Add(new HL7Fields(HL7FieldName.Security_MSH, enc.GetBytes(strArrAux[7])));
                  }
                  if ((strArrAux.Length > 8) && (strArrAux[8] != "") && (strArrAux[8] != null)) // MessageType_MSH = 00009,
                  {
                      CLogger.Append("Field 8: " + strArrAux[8], CLogger.INFO);
                      System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                      lstHL7Fields.Add(new HL7Fields(HL7FieldName.MessageType_MSH, enc.GetBytes(strArrAux[8])));
                  }
                  if ((strArrAux.Length > 9) && (strArrAux[9] != "") && (strArrAux[9] != null))// MessageControlID_MSH = 00010,
                  {
                      CLogger.Append("Field 9: " + strArrAux[9], CLogger.INFO);
                      System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                      lstHL7Fields.Add(new HL7Fields(HL7FieldName.MessageControlID_MSH, enc.GetBytes(strArrAux[9])));
                  }
                  if ((strArrAux.Length > 10) && (strArrAux[10] != "") && (strArrAux[10] != null)) // ProcessingID_MSH = 00011,
                  {
                      CLogger.Append("Field 10: " + strArrAux[10], CLogger.INFO);
                      System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                      lstHL7Fields.Add(new HL7Fields(HL7FieldName.ProcessingID_MSH, enc.GetBytes(strArrAux[10])));
                  }
                  if ((strArrAux.Length > 11) && (strArrAux[11] != "") && (strArrAux[11] != null)) // VersionID_MSH = 00012,
                  {
                      CLogger.Append("Field 11: " + strArrAux[11], CLogger.INFO);
                      System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                      lstHL7Fields.Add(new HL7Fields(HL7FieldName.VersionID_MSH, enc.GetBytes(strArrAux[11])));
                  }
                  if ((strArrAux.Length > 12) && (strArrAux[12] != "") && (strArrAux[12] != null)) // SequenceNumber_MSH = 00013,
                  {
                      CLogger.Append("Field 12: " + strArrAux[12], CLogger.INFO);
                      System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                      lstHL7Fields.Add(new HL7Fields(HL7FieldName.SequenceNumber_MSH, enc.GetBytes(strArrAux[12])));
                  }
                  if ((strArrAux.Length > 13) && (strArrAux[13] != "") && (strArrAux[13] != null)) //ContinuationPointer_MSH = 00014,
                  {
                      CLogger.Append("Field 13: " + strArrAux[13], CLogger.INFO);
                      System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                      lstHL7Fields.Add(new HL7Fields(HL7FieldName.ContinuationPointer_MSH, enc.GetBytes(strArrAux[13])));
                  }
                  if ((strArrAux.Length > 14) && (strArrAux[14] != "") && (strArrAux[14] != null)) //  AcceptAknowledgmentType_MSH = 00015,
                  {
                      CLogger.Append("Field 14: " + strArrAux[14], CLogger.INFO);
                      System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                      lstHL7Fields.Add(new HL7Fields(HL7FieldName.AcceptAknowledgmentType_MSH, enc.GetBytes(strArrAux[14])));
                  }
                  if ((strArrAux.Length > 15) && (strArrAux[15] != "") && (strArrAux[15] != null)) // ApplicationAknowledgmentType_MSH = 00016,
                  {
                      CLogger.Append("Field 15: " + strArrAux[15], CLogger.INFO);
                      System.Text.ASCIIEncoding enc = new ASCIIEncoding();                      
                      lstHL7Fields.Add(new HL7Fields(HL7FieldName.ApplicationAknowledgmentType_MSH, enc.GetBytes(strArrAux[15])));                      
                  }                 

                  if ((strArrAux.Length > 16) && (strArrAux[16] != null) && (strArrAux[16] != "")) //CountryCode_MSH=00017,
                  {
                      CLogger.Append("Field 16: " + strArrAux[16], CLogger.INFO);
                      System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                      lstHL7Fields.Add(new HL7Fields(HL7FieldName.CountryCode_MSH, enc.GetBytes(strArrAux[16])));
                  }
                  if ((strArrAux.Length > 17)&&(strArrAux[17] != null) && (strArrAux[17] != "")) // CharacterSet_MSH=00692,
                  {
                      CLogger.Append("Field 17: " + strArrAux[17], CLogger.INFO);
                      System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                      lstHL7Fields.Add(new HL7Fields(HL7FieldName.CharacterSet_MSH, enc.GetBytes(strArrAux[17])));
                  }
                  if ((strArrAux.Length > 18) && (strArrAux[18] != null) && (strArrAux[18] != "")) // PrincipalLanguageofMessage_MSH=00693,
                  {
                      CLogger.Append("Field 18: " + strArrAux[18], CLogger.INFO);
                      System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                      lstHL7Fields.Add(new HL7Fields(HL7FieldName.PrincipalLanguageofMessage_MSH, enc.GetBytes(strArrAux[18])));
                  }
                  if ((strArrAux.Length > 19) && (strArrAux[19] != null) && (strArrAux[19] != "")) // AlternateCharacterSetHandlingScheme_MSH=01317,
                  {
                      CLogger.Append("Field 19: " + strArrAux[19], CLogger.INFO);
                      System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                      lstHL7Fields.Add(new HL7Fields(HL7FieldName.AlternateCharacterSetHandlingScheme_MSH, enc.GetBytes(strArrAux[19])));
                  }
                  if ((strArrAux.Length > 20 ) && (strArrAux[20] != null) && (strArrAux[20] != "")) //MessageProfileIdentifier_MSH=01598,
                  {
                      CLogger.Append("Field 20: " + strArrAux[20], CLogger.INFO);
                      System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                      lstHL7Fields.Add(new HL7Fields(HL7FieldName.MessageProfileIdentifier_MSH, enc.GetBytes(strArrAux[20])));
                  }
              }
              catch (Exception ex)
              {
                  
                  CLogger.Append("ERROR al descomponer Segmento MSH Excepcion: "+ex.Message , CLogger.INFO);
                  throw new Exception("EXCEPCION AL DESCOMPONER SEGMENTO MSH");
              }

            CLogger.Append("**********************FINAL DEL DESCOMPONER SEGMENTO MSH OK*******************", CLogger.INFO);

              //int indice=0;
             /*
              int y = 0;  //CANTIDAD DE CAMPOS y barras              
              strArrAux[y] = null;
              for (int x = 0; x <= strSegment.Length; x++)
              {
                  if (strSegment[x] != '|')
                  {
                      strArrAux[y] = strArrAux[y] + strSegment[x];
                      
                  }
                  else
                  {
                      CLogger.Append(strArrAux[y], CLogger.INFO);                      
                      y++;
                      strArrAux[y] = null;
                  }
                  x++;

              }

              j = 0;
              foreach (string strField in strArrAux)
              {
                  CLogger.Append("Field " + j.ToString() + " : " + strField, CLogger.INFO);
                  j++;
              }
              //HL7Fields obj;
            */



              return lstHL7Fields;

          }
        #endregion
        /*
        public HL7SegmentMSH():base(HL7MessageName.MessageMSH)
        {

        }
   
        public int BuildFields(byte[] byteInputStreamParam, int intOffset)
        {
            Debug.WriteLine("[MSH:BuildFields] Segmento es " + HomeroFormatter.HomeroFormatter.GetString(byteInputStreamParam, byteInputStreamParam.Length-intOffset,intOffset));
            throw new NotImplementedException();
        }
         * */
    }
}
